package xyz.zyt123.videowall;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Tab;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import xyz.zyt123.videowall.bean.ErrorMsg;
import xyz.zyt123.videowall.bean.ErrorMsgProperties;
import xyz.zyt123.videowall.component.VideoView;
import xyz.zyt123.videowall.dao.Dao;
import xyz.zyt123.videowall.util.ConfigUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.ResourceBundle;

public class MainController implements Initializable {
    @FXML
    public GridPane videoGrid;
    private final List<VideoView> videoViews = new ArrayList<>();
    public TextField imageFileAddress;
    public TextField video1;
    public TextField video2;
    public TextField video3;
    public TextField video4;
    public TextField video5;
    public TextField video6;
    public TextField video7;
    public TextField video8;
    public TextField video9;
    public TextField video10;
    public TextField video11;
    public TextField video12;
    public TableView<ErrorMsgProperties> historyTable;
    public Tab historyTab;
    public TextField videoFileAddress;


    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        Properties config = ConfigUtil.getConfig();
        video1.setText(config.getProperty("file1"));
        video2.setText(config.getProperty("file2"));
        video3.setText(config.getProperty("file3"));
        video4.setText(config.getProperty("file4"));
        video5.setText(config.getProperty("file5"));
        video6.setText(config.getProperty("file6"));
        video7.setText(config.getProperty("file7"));
        video8.setText(config.getProperty("file8"));
        video9.setText(config.getProperty("file9"));
        video10.setText(config.getProperty("file10"));
        video11.setText(config.getProperty("file11"));
        video12.setText(config.getProperty("file12"));
        imageFileAddress.setText(config.getProperty("imageFileAddress"));
        videoFileAddress.setText(config.getProperty("videoFileAddress"));
        for (int i = 0; i < 12; i++) {
            VideoView node = new VideoView("视频" + (i + 1));
            videoViews.add(node);
            videoGrid.add(node, i % 4, i / 4);
        }

        historyTab.selectedProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue) {
                List<ErrorMsgProperties> all = Dao.findAll();
                historyTable.getItems().clear();
                historyTable.getItems().addAll(all);
            }
        });
    }

    public void videoHandler(ActionEvent actionEvent) {
        Properties config = ConfigUtil.getConfig();
        Button source = (Button) actionEvent.getSource();
        if (source.getText().equals("开始监控")) {
            for (int i = 0; i < videoViews.size(); i++) {
                VideoView node = videoViews.get(i);
                node.setUrl(config.getProperty("file" + (i + 1)));
                node.start();
            }
            source.setText("停止监控");
        } else {
            videoViews.forEach(VideoView::stop);
            source.setText("开始监控");
        }
    }

    public void onSaveConfig() {
        Properties config = ConfigUtil.getConfig();
        config.setProperty("file1", video1.getText());
        config.setProperty("file2", video2.getText());
        config.setProperty("file3", video3.getText());
        config.setProperty("file4", video4.getText());
        config.setProperty("file5", video5.getText());
        config.setProperty("file6", video6.getText());
        config.setProperty("file7", video7.getText());
        config.setProperty("file8", video8.getText());
        config.setProperty("file9", video9.getText());
        config.setProperty("file10", video10.getText());
        config.setProperty("file11", video11.getText());
        config.setProperty("file12", video12.getText());
        config.setProperty("imageFileAddress", imageFileAddress.getText());
        config.setProperty("videoFileAddress", videoFileAddress.getText());
        ConfigUtil.saveConfig(config);
    }

    public void saveVideo() {
        DateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
        Properties config = ConfigUtil.getConfig();
        String videoFileAddress = ConfigUtil.getVideoFileAddress();
        for (int i = 1; i <= 12; i++) {
            String filePath = config.getProperty("file" + (i));
            File file = new File(filePath);
            try {
                new File(videoFileAddress + "//" + "camera" + i + "/").mkdirs();
                Files.copy(file.toPath(), new FileOutputStream(videoFileAddress + "//" + "camera" + i + "/" + formatter.format(new Date()) + ".mp4"));
            } catch (IOException ignored) {
            }
        }

    }
}